package com.ea.admin.service;

import com.ea.common.gateway.GatewayFeign;
import com.ea.common.report.ReportFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Service
@Slf4j
public class RestartService {

    @Resource
    ReportFeign reportFeign;

    @Resource
    GatewayFeign gatewayFeign;

    @Value("${server.port}")
    private String port;

    public void restart() {
        // 创建 RestTemplate 实例
        RestTemplate restTemplate = new RestTemplate();

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", "application/json"); // 明确设置 Content-Type

        // 创建请求实体
        HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);

        // 调用本地端点重启服务
        String url = "http://localhost:" + port + "/actuator/restart"; // 本地端点 URL
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);

        if (response.getStatusCode().is2xxSuccessful()) {
            System.out.println("Restart initiated successfully.");
        } else {
            System.out.println("Failed to initiate restart.");
        }
    }

    public void restartAll() {
        // TODO 调用下方代码重启后report报错401
//        reportFeign.restart();
        gatewayFeign.restart();
        restart();
    }
}
